package com.example.t1;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class BaseViewActivity extends FragmentActivity implements
	OnClickListener {
    private boolean DEBUG = true;

    public LinearLayout contentLayout;

    private FrameLayout titleLayout;

    private TextView title;

    public LayoutInflater inflater;

    /**
     * 适应性强，可以写文字，button有主题，除掉主题比较麻烦
     */
    private TextView r1Button;
    private TextView leftButton;

    public final int DIALOG_LOADING = 1, DIALOG_ALERT = 2;

    public Resources resources;

    private String dialogTitle, DialogMsg;
    private View.OnClickListener okListener;

    private boolean isCover = false;

    /**
     * shareperfrence
     */
    // public SharePre share;

    /**
     * 已经实例化了并且加入缓存，如果不想用到请清空
     */
    // public ImageFetcher fetcher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
	// 取消状态栏
	// this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
	// WindowManager.LayoutParams.FLAG_FULLSCREEN);
	super.onCreate(savedInstanceState);
	// setContentView(R.layout.activity_base);
	// init();
    }

    @Override
    public void setContentView(int layoutResID) {
	super.setContentView(R.layout.activity_base);
	init();
	contentLayout.addView(inflater.inflate(layoutResID, null),
		LinearLayout.LayoutParams.FILL_PARENT,
		LinearLayout.LayoutParams.FILL_PARENT);
    }

    /**
     * 初始化界面布局资源
     */
    private void init() {

	// share = SharePre.getInstance(this);
	resources = getResources();
	inflater = LayoutInflater.from(this);

	// fetcher.setImageCache(new ImageCache(this, "shgooCache"));
	contentLayout = (LinearLayout) findViewById(R.id.contentLayout);
	titleLayout = (FrameLayout) findViewById(R.id.titleLayout);
	title = (TextView) findViewById(R.id.titileText);
	// title.setTypeface(Typeface.createFromAsset(this.getAssets(),
	// "fonts/FZZYJW.TTF"));

	leftButton = (TextView) findViewById(R.id.leftButton);
	r1Button = (TextView) findViewById(R.id.r1Button);

	leftButton.setOnClickListener(this);
	r1Button.setOnClickListener(this);

	ViewTreeObserver vto2 = titleLayout.getViewTreeObserver();
	vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
	    @Override
	    public void onGlobalLayout() {
		titleLayout.getViewTreeObserver().removeGlobalOnLayoutListener(
			this);
		if (!isCover) {
		    // showToast(titleLayout.getHeight()+"");
		    // -1 代表填充父控件
		    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
			    -1, -1);
		    params.topMargin = titleLayout.getHeight();
		    contentLayout.setLayoutParams(params);
		}
	    }
	});
    }

    @Override
    protected void onResume() {
	super.onResume();

    }

    public void setCoverContent(boolean flag) {
	isCover = flag;
    }

    /**
     * 判断网络是否可用
     * 
     * @param context
     * @return
     */
    public boolean isNetworkConnected(Context context) {
	if (context != null) {
	    ConnectivityManager mConnectivityManager = (ConnectivityManager) context
		    .getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo mNetworkInfo = mConnectivityManager
		    .getActiveNetworkInfo();
	    if (mNetworkInfo != null) {
		return mNetworkInfo.isAvailable();
	    }
	}
	return false;
    }

    /**
     * 
     * 描述：设置右边按钮文字
     * 
     * @param txt
     * @throws
     * @date：2013-12-4 上午10:16:45
     * @author 智迅 zhixun123@gmail.com
     * @version v1.0
     */
    public void setR1txt(String txt) {
	r1Button.setText(txt);
    }

    /**
     * 设置左边文本--修改2013年12月11日9:41:35 （之前为ImageButton，根据需求变动更改）
     * 
     * @param text
     */
    public void setLeftText(String text) {
	leftButton.setText(text);
    }

    /**
     * 
     * 描述：设置右边按钮字体大小
     * 
     * @throws
     * @date：2013-12-4 上午10:19:51
     * @author 智迅 zhixun123@gmail.com
     * @version v1.0
     */
    public void setR1size(int size) {
	r1Button.setTextSize(size);
    }

    /**
     * 
     * 描述：设置右边按钮文字颜色
     * 
     * @param 16进制
     * @throws
     * @date：2013-12-4 上午10:16:45
     * @author 智迅 zhixun123@gmail.com
     * @version v1.0
     */
    public void setR1color(int color) {
	r1Button.setTextColor(color);
    }

    /**
     * 
     * 描述：设置标题栏
     * 
     */
    @Override
    public void setTitle(CharSequence str) {
	title.setText(str);
    }

    /**
     * 
     * 描述：给标题栏设置背景
     * 
     * @throws
     * @date：2013-11-14 下午2:25:41
     * @author 智迅 zhixun123@gmail.com
     * @version v1.0
     */

    public void setTitleBg(int rId) {
	titleLayout.setBackgroundResource(rId);
    }

    /**
     * 
     * 描述：是否隐藏标题栏false是隐藏
     * 
     */
    public void setTitileVisibale(boolean flag) {
	if (flag) {
	    titleLayout.setVisibility(View.VISIBLE);
	} else {
	    titleLayout.setVisibility(View.GONE);
	}
    }

    /**
     * 
     * 设置左上角 按钮事件，默认是退出
     */
    public void setLeftButtonClick() {
	finish();
    }

    /**
     * 
     * 设置又上交按钮背景，默认都是隐藏
     * 
     */
    public void setLeftBg(int bgId) {

	leftButton.setBackgroundResource(bgId);
    }

    public void setR1Bg(int bgId) {
	if (!r1Button.isShown()) {
	    r1Button.setVisibility(View.VISIBLE);
	}
	r1Button.setBackgroundResource(bgId);
    }

    /**
     * 
     * 显示右上角按钮
     * 
     */
    public void setR1Visibale(int flag) {

	r1Button.setVisibility(flag);
    }

    /**
     * 
     * 描述：给右上角按钮添加事件
     * 
     */
    public void setR1ButtonClick() {

    }

    /**
     * 
     * 描述：子类的复写onclick方法必须加super，否则头标题左右按钮无法使用
     * 
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     * @author: 智讯 zhixun123@gmail.com
     * @date：2013-12-5 上午9:05:35
     * @version v1.0
     */
    @Override
    public void onClick(View v) {

	switch (v.getId()) {
	case R.id.leftButton:
	    setLeftButtonClick();
	    break;
	case R.id.r1Button:
	    setR1ButtonClick();
	    break;
	default:
	    break;
	}
    }

    /**
     * 
     * 弹出框
     * 
     * @param msg
     * @throws
     * @date 2012-7-27 10:02:28
     */
    public void showToast(String msg) {
	Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    public void showToast(int id) {
	Toast.makeText(this, id, Toast.LENGTH_SHORT).show();
    }

    /**
     * 
     * 带确定和取消按钮的弹出框
     */
    public void showAlertDialog(String msg, OnClickListener clicker) {
	this.DialogMsg = msg;
	this.okListener = clicker;
	showDialog(DIALOG_ALERT);
    }

    /**
     * 当一个布局中有两个listview的时候，两个listview都不会正常显示，只会显示一格。加上这个方法就可以了
     * 
     * @param listView
     * @throws
     */
    public void setListViewHigh(ListView listView) {
	ListAdapter listAdapter = listView.getAdapter();
	if (listAdapter == null) {
	    return;
	}
	int totalHeight = 0;
	int count = listAdapter.getCount();

	for (int i = 0; i < count; i++) {
	    View listItem = listAdapter.getView(i, null, listView);
	    listItem.measure(0, 0);
	    totalHeight += listItem.getMeasuredHeight();
	}

	ViewGroup.LayoutParams params = listView.getLayoutParams();
	if (count == 0) {
	    View listItem = listAdapter.getView(0, null, listView);
	    listItem.measure(0, 0);
	    totalHeight += listItem.getMeasuredHeight();
	    params.height = totalHeight;
	} else {
	    params.height = totalHeight
		    + (listView.getDividerHeight() * (count - 1));
	}
	((MarginLayoutParams) params).setMargins(0, 10, 0, 10);
	// ((MarginLayoutParams) params).setMargins(0, 0, 0, 0);
	listView.setLayoutParams(params);

    }

    /**
     * 
     * 描述 增加设置间隔功能
     * 
     * @param listView
     * @param left
     * @param top
     * @param right
     * @param bottom
     * @throws
     * @date：2013-12-10 下午4:32:46
     * @author 智迅 zhixun123@gmail.com
     */
    public void setListViewHigh(ListView listView, int left, int top,
	    int right, int bottom) {
	ListAdapter listAdapter = listView.getAdapter();
	if (listAdapter == null || listView.getChildCount() < 1) {
	    return;
	}
	int totalHeight = 0;
	int count = listAdapter.getCount();

	for (int i = 0; i < count; i++) {
	    View listItem = listAdapter.getView(i, null, listView);
	    listItem.measure(0, 0);
	    totalHeight += listItem.getMeasuredHeight();
	}

	ViewGroup.LayoutParams params = listView.getLayoutParams();
	if (count == 0) {
	    View listItem = listAdapter.getView(0, null, listView);
	    listItem.measure(0, 0);
	    totalHeight += listItem.getMeasuredHeight();
	    params.height = totalHeight;
	} else {
	    params.height = totalHeight
		    + (listView.getDividerHeight() * (count - 1));
	}
	((MarginLayoutParams) params).setMargins(left, top, right, bottom);
	listView.setLayoutParams(params);

    }

    /**
     * @param listView
     * @throws
     * @date 2012-7-16 03:29:17
     */
    public void setListViewHigh(ListView listView, int num) {
	ListAdapter listAdapter = listView.getAdapter();
	if (listAdapter == null || listView.getChildCount() < 1) {
	    return;
	}
	int totalHeight = 0;
	View listItem = listAdapter.getView(0, null, listView);
	listItem.measure(0, 0);
	totalHeight = listItem.getMeasuredHeight() * num;

	ViewGroup.LayoutParams params = listView.getLayoutParams();
	params.height = totalHeight + (listView.getDividerHeight() * (num - 1));
	((MarginLayoutParams) params).setMargins(10, 10, 10, 10);
	listView.setLayoutParams(params);
    }

    /**
     * 
     * 带输入框的对话框
     * 
     */
    private void showInputDialog(String msg,
	    DialogInterface.OnClickListener clicker) {
	new AlertDialog.Builder(this).setTitle(msg)
		.setIcon(android.R.drawable.ic_dialog_info)
		.setView(new EditText(this)).setPositiveButton("确定", clicker)
		.setNegativeButton("取消", new DialogInterface.OnClickListener() {

		    @Override
		    public void onClick(DialogInterface dialog, int which) {
			dialog.dismiss();
		    }
		}).show();
    }

    public void myLog(String str) {
	if (DEBUG) {
	    Log.d(getClass().getName(), str);
	}
    }
}
